<?php

require_once 'application/models/domain/User.php';

class MY_PortalController extends Controller {

	//
	var $user_logued=NULL;
    //
	var $branch_cn = BRANCH_ID;
    var $cert_file;
    var $key_file;
	var  $api;

	function MY_PortalController(){
		parent::Controller();

	    // Services Control specific logic ------------------------------------
        //
        $this->branch_cn = BRANCH_ID;
        $this->key_file  = CERTIFICATES_PATH. $this->branch_cn .'-key.pem';
        $this->cert_file = CERTIFICATES_PATH. $this->branch_cn .'-cert.pem';

		//Load util
		$this->load->helper('url');            //Este helper nos da las funcionalidades de base_url, current_url, uri_string
		$this->load->library('session');       //Informacion de la sesion
		$this->load->database();
	}

	function load_api(){
		if($this->api==NULL){
			require_once('goalbit/sdks/services-api/php/goalbit-services-api.php');
			$this->api = new GoalbitServicesAPI( CA_PUBLIC_CERTIFICATE, $this->cert_file, $this->key_file );
		}
	}

	function showError($error){
		$this->load->view("error",array("error"=>$error));
	}


	function setUserLogued(User $user){
		if($user==NULL){
			$this->session->unset_userdata('user');
		}else{
			$this->session->set_userdata('user', json_encode($user));
		}
		$user_logued = $user;
	}

	function getUserLogued() {

		if($user_logued==NULL){
			$user_json = json_decode($this->session->userdata('user'));
			if (!isset($user_json)) {
				return NULL;
			}
			$user_logued = new User();
			$user_logued->id = $user_json->{"id"};
			$user_logued->nick = $user_json->{'nick'};
			$user_logued->login = $user_json->{'login'};
			$user_logued->password = $user_json->{'password'};
			$user_logued->create_date = NULL;
			$user_logued->suite_cert_cn = $user_json->{'suite_cert_cn'};
			$user_logued->is_admin = $user_json->{'is_admin'};
		}
		return $user_logued;

	}

	function setPCType($pc_type){
		$this->session->set_userdata('pc_type',$pc_type);
	}
	function getPCType(){
		return $this->session->userdata('pc_type');
	}

	function returnJson($ret){
		header ("content-type: text/json");
		echo json_encode($ret);
	}

	function getDataRet($ok,$msg=""){
		return array("ok"=>$ok,"msg"=>$msg);
	}

		
}

?>
